<?php
if(defined("IZ_ACCNAME")) {
if($calmode == 'mysql') {
  if(function_exists('mysqli_connect')) {
    global $dblink;
    include($_SESSION['IZ_CALPATH'].'includes/mysql.php');
    if(!$dblink->query($createcats)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($createcomments)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($createevents)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($createsettings)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($createtodo)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($createusers)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($creategroups)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($inscats)) { $error .= mysqli_error($dblink).'<br />'."\n"; }  
    if(!$dblink->query($insusers)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($insgroups)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
    if(!$dblink->query($inssettings)) { $error .= mysqli_error($dblink).'<br />'."\n"; }
  } else {
    include($_SESSION['IZ_CALPATH'].'includes/mysql.php');
    if(!mysql_query($createcats)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($createcomments)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($createevents)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($createsettings)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($createtodo)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($createusers)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($creategroups)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($inscats)) { $error .= mysql_error().'<br />'."\n"; }  
    if(!mysql_query($insusers)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($insgroups)) { $error .= mysql_error().'<br />'."\n"; }
    if(!mysql_query($inssettings)) { $error .= mysql_error().'<br />'."\n"; }
  }
} elseif($calmode == 'postgre') {
    if(!pg_query('CREATE TABLE "categories" (
      "id" varchar(8) NOT NULL,
      "name" text NOT NULL,
      "owner" text NOT NULL,
      "type" text NOT NULL, 
      "public" int2 NOT NULL DEFAULT 1,
      "color" text NOT NULL, 
          PRIMARY KEY ("id")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
    if(!pg_query('CREATE TABLE "comments" (
      "id" varchar(8) NOT NULL,
      "postid" varchar(8) NOT NULL,
      "author" text NOT NULL,
      "posted" numeric(11) NOT NULL DEFAULT 0,
      "text" text NOT NULL,
          PRIMARY KEY ("id")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
    if(!pg_query('CREATE TABLE "events"(
      "id" varchar(8) NOT NULL,
      "category" text NOT NULL,
      "eventstart" numeric(11) NOT NULL,
      "eventend" numeric(11) NOT NULL,
      "title" text NOT NULL,
      "description" text,
      "venue" text,
      "linkgoogle" int2 NOT NULL,
      "allday" int2 NOT NULL,
      "instanceof" text,
      "status" text,
      "author" text,
      "cust1" text,
      "cust2" text,
      "cust3" text,
      "attendees" text,
      "comments" int2 NOT NULL,
      "icon" int2 DEFAULT -1,
          PRIMARY KEY ("id")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
    if(!pg_query('CREATE TABLE "settings" ( 
      "name" varchar(30) NOT NULL,
      "setval" text NOT NULL,
      CONSTRAINT uniquename UNIQUE("name")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
    if(!pg_query('CREATE TABLE "tasks" (
      "id" varchar(8) NOT NULL,
      "category" text NOT NULL,
      "created" numeric(11) NOT NULL DEFAULT 0,
      "due" numeric(11) NOT NULL DEFAULT 0,
      "title" text NOT NULL,
      "description" text,
      "author" text NOT NULL,
      "complete" int2 NOT NULL DEFAULT 0,
      "cust1" text,
      "cust2" text,
      "cust3" text,
      "comments" int2 NOT NULL DEFAULT 0,
      "status" text,
      "icon" int2 DEFAULT -1,
          PRIMARY KEY("id")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
    if(!pg_query('CREATE TABLE "users" (
      "id" varchar(8) NOT NULL,
      "name" text NOT NULL,
      "usergroup" text NOT NULL ,
      "password" text NOT NULL,
      "language" text NOT NULL,
      "daysize" text NOT NULL,
      "firstdayofweek" int2 NOT NULL DEFAULT 0,
      "theme" text NOT NULL,
      "defaultstatus" text NOT NULL,
      "showall" int2 NOT NULL DEFAULT 1,
      "dateformat" text NOT NULL,
      "defaultcategoryevents" text,
      "defaultcategorytasks" text,
      "pageafter" integer NOT NULL DEFAULT 30,
      "fontsize" text NOT NULL,
      "collapsestatus" text NOT NULL,
      "color" text NOT NULL,
      "viewdetails" int2 NOT NULL DEFAULT 1,
      "includecomplete" int2 NOT NULL DEFAULT 1,
          PRIMARY KEY("name")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
    if(!pg_query('CREATE TABLE "groups" (
      "id" varchar(8) NOT NULL,
      "name" text NOT NULL,
      "view" int2 NOT NULL DEFAULT 1,
      "edit" int2 NOT NULL DEFAULT 1,
      "delete" int2 NOT NULL DEFAULT 1,
      "cpanel" int2 NOT NULL DEFAULT 0,
      "customise" int2 NOT NULL DEFAULT 1,
      "comment" int2 NOT NULL DEFAULT 1,
          PRIMARY KEY("name")) WITHOUT OIDS;')) { $error .= pg_last_error().'<br />'."\n"; }
  if(!pg_query('INSERT INTO "categories" ("id", "name", "owner", "type", "public", "color") VALUES'."
      ('psdfsgtr','".STR_EVENTS."','everyone','event','1','#FFF6CF'),
      ('ufbqdfkr','".STR_TASKS."','everyone','task','1','#FFF6CF');")) { $error .= pg_last_error().'<br />'."\n"; }
  if(!pg_query('INSERT INTO "users" ("id","name","usergroup","password","language","daysize","firstdayofweek","theme","defaultstatus","showall","dateformat","defaultcategoryevents","defaultcategorytasks","pageafter","fontsize","collapsestatus","color","viewdetails","includecomplete") VALUES '."
    ('ifhhn90s','$adminuser','ijklmnip','$md5pass','$lang','small','0','default','Public','1','d-m-y','psdfsgtr','ufbqdfkr','30','medium','0-0-1','default','1','1'),
    ('ifhhn99s','usermask','ijklmnip','".saltpassword('1')."','en','small','0','default','Public','1','d-m-y','','','30','medium','0-0-1','default','1','1'),
    ('zzzzzzzz','everyone','abcdefgh','','$lang', 'small', '0', 'default','Public','1','d-m-y','psdfsgtr','ufbqdfkr','30','medium','0-0-1','default','1','1');")) { $error .= pg_last_error().'<br />'."\n"; }
  if(!pg_query('INSERT INTO "groups" ("id","name","view","edit","delete","cpanel","comment","customise") VALUES'."
    ('ijklmnip','".STR_ADMINISTRATORS."','1','2','2','1','1','1'),
    ('abcdefgh','".STR_GUESTS."','1','1','1','0','1','1');")) { $error .= pg_last_error().'<br />'."\n"; }
  if(!pg_query('INSERT INTO "settings" ("name", "setval") VALUES '."
    ('IZ_CALURL', '$calurl'),
    ('IZ_REPLACELOGO','0'),
    ('IZ_CURTHEME', 'default'),
    ('IZ_ALLOWCOMMENTS','1'),
    ('IZ_OPT','4095'),
    ('IZ_NV','8191'),
    ('IZ_DEFAULTGROUP','abcdefgh'),
    ('IZ_REWRITE','0'),
    ('IZ_LANG', '$lang'),
    ('IZ_FIRSTDAY', '0'),
    ('IZ_MVIEWDAYSIZE', 'small'),
    ('IZ_CALNAME', '$calendarname'),
    ('IZ_TIMEOFFSET', '0'),
    ('IZ_INVITEMODE', 'php'),
    ('IZ_GMAIL_ACC', ''),
    ('IZ_GMAIL_PWD', ''),
    ('IZ_DEFNUMRECS', '10'),
    ('IZ_DEFAULTVIEW', 'month'),
    ('IZ_ITEMCOLORS', 'c'),
    ('cust1', 'none'),
    ('cust2', 'none'),
    ('cust3', 'none'),
    ('cust_pagebg', ''),
    ('cust_calbg', ''),
    ('cust_occbg', ''),
    ('cust_occtext', ''),
    ('cust_blankbg', ''),
    ('cust_blanktext', ''),
    ('cust_todaybg', ''),
    ('cust_todaytext', ''),
    ('cust_fillerdays', ''),
    ('cust_calwrappertext', ''),
    ('cust_boxbg', ''),
    ('cust_boxtext', ''),
    ('cust_othertext', '');")) { $error .= pg_last_error().'<br />'."\n"; }
} elseif($calmode == 'msq') {
    if(!mssql_query('CREATE TABLE categories (
      id varchar(8) NOT NULL,
      name varchar(8) NOT NULL,
      owner varchar(8) NOT NULL,
      type varchar(8) NOT NULL,
      [public] int NOT NULL,
      [color] varchar(8) NOT NULL,
          PRIMARY KEY (id))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
    if(!mssql_query('CREATE TABLE comments (
      id varchar(8) NOT NULL,
      postid varchar(8) NOT NULL,
      author varchar(20) NOT NULL,
      posted numeric(11) NOT NULL,
      [text] text NOT NULL,
          PRIMARY KEY (id))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
    if(!mssql_query('CREATE TABLE events (
      id varchar(8) NOT NULL,
      category varchar(8) NOT NULL,
      eventstart numeric(11) NOT NULL,
      eventend numeric(11) NOT NULL,
      title varchar(100) NOT NULL,
      description text,
      venue varchar(30),
      linkgoogle int NOT NULL,
      allday int NOT NULL,
      instanceof varchar(20),
      status varchar(20),
      author varchar(25),
      cust1 text,
      cust2 text,
      cust3 text,
      attendees text,
      comments varchar(20),
      icon varchar(2),
          PRIMARY KEY (id))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
    if(!mssql_query('CREATE TABLE settings ( 
      name varchar(30) NOT NULL,
      setval text NOT NULL,
      CONSTRAINT uniquename UNIQUE(name))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
    if(!mssql_query('CREATE TABLE tasks (
      id varchar(8) NOT NULL,
      category varchar(8) NOT NULL,
      created numeric(11) NOT NULL DEFAULT 0,
      due numeric(11) NOT NULL DEFAULT 0,
      title text NOT NULL,
      description text,
      author varchar(20) NOT NULL,
      complete int NOT NULL DEFAULT 0,
      cust1 text,
      cust2 text,
      cust3 text,
      comments varchar(20),
      status varchar(20),
      icon varchar(2),
          PRIMARY KEY(id))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
    if(!mssql_query('CREATE TABLE users (
      id varchar(8) NOT NULL,
      name varchar(20) NOT NULL ,
      usergroup varchar(20) NOT NULL ,
      password varchar(40) NOT NULL ,
      [language] text NOT NULL,
      daysize text NOT NULL,
      firstdayofweek int NOT NULL DEFAULT 0,
      theme text NOT NULL,
      defaultstatus text NOT NULL,
      showall int NOT NULL DEFAULT 1,
      dateformat text NOT NULL,
      defaultcategoryevents varchar(8),
      defaultcategorytasks varchar(8),
      pageafter int NOT NULL DEFAULT 30,
      fontsize text NOT NULL,
      collapsestatus text NOT NULL,
      color varchar(8),
      viewdetails int NOT NULL DEFAULT 1,
      includecomplete int NOT NULL DEFAULT 1,
          PRIMARY KEY(id))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
    if(!mssql_query('CREATE TABLE groups (
      id varchar(8) NOT NULL,
      name varchar(20) NOT NULL ,
      [view] int NOT NULL DEFAULT 1,
      edit int NOT NULL DEFAULT 1,
      [delete] int NOT NULL DEFAULT 0,
      cpanel int NOT NULL DEFAULT 0,
      customise int NOT NULL DEFAULT 1,
      comment int NOT NULL DEFAULT 1,
          PRIMARY KEY(id))')) { $error .= mssql_get_last_message().'<br />'."\n"; }
  if(!mssql_query("INSERT INTO categories (id, name, owner, type,[public],[color])
    SELECT 'psdfsgtr','".STR_EVENTS."','everyone','event','1','#FFF6CF' UNION ALL
    SELECT 'ufbqdfkr','".STR_TASKS."','everyone','task','1','#FFF6CF'")) { $error .= mssql_get_last_message().'<br />'."\n"; }
  if(!mssql_query("INSERT INTO users (id,name,usergroup,password,[language],daysize,firstdayofweek,theme,defaultstatus,showall,dateformat,defaultcategoryevents,defaultcategorytasks,pageafter,fontsize,collapsestatus,color,viewdetails,includecomplete)
    SELECT 'ifhhn90s','$adminuser','ijklmnip','$md5pass','$lang','small','0','default','Public','1','d-m-y','psdfsgtr','ufbqdfkr','30','medium','0-0-1','default','1','1'  UNION ALL
    SELECT 'ifhhn99s','usermask','ijklmnip','".saltpassword('1')."','en','small','0','default','Public','1','d-m-y','','','30','medium','0-0-1','default','1','1' UNION ALL
    SELECT 'zzzzzzzz','Everyone','abcdefgh','','$lang', 'small', '0', 'default','Public','1','d-m-y','psdfsgtr','ufbqdfkr','30','medium','0-0-1','default','1','1'")) { $error .= mssql_get_last_message().'<br />'."\n"; }
  if(!mssql_query("INSERT INTO groups (id,name,[view],edit,[delete],cpanel,comment,customise)
    SELECT 'ijklmnip','".STR_ADMINISTRATORS."','1','2','2','1','1','1' UNION ALL
    SELECT 'abcdefgh','".STR_GUESTS."','1','1','1','0','1','1';")) { $error .= mssql_get_last_message().'<br />'."\n"; }
  if(!mssql_query("INSERT INTO settings (name, setval)
    SELECT 'IZ_CALURL', '$calurl' UNION ALL
    SELECT 'IZ_REPLACELOGO','0' UNION ALL
    SELECT 'IZ_CURTHEME', 'default' UNION ALL
    SELECT 'IZ_ALLOWCOMMENTS','1' UNION ALL
    SELECT 'IZ_OPT','4095' UNION ALL
    SELECT 'IZ_NV','8191' UNION ALL
    SELECT 'IZ_DEFAULTGROUP','abcdefgh' UNION ALL
    SELECT 'IZ_REWRITE','0' UNION ALL
    SELECT 'IZ_LANG', '$lang' UNION ALL
    SELECT 'IZ_FIRSTDAY', '0' UNION ALL
    SELECT 'IZ_MVIEWDAYSIZE', 'small' UNION ALL
    SELECT 'IZ_CALNAME', '$calendarname' UNION ALL
    SELECT 'IZ_TIMEOFFSET', '0' UNION ALL
    SELECT 'IZ_INVITEMODE', 'php' UNION ALL
    SELECT 'IZ_GMAIL_ACC', '' UNION ALL
    SELECT 'IZ_GMAIL_PWD', '' UNION ALL
    SELECT 'IZ_DEFNUMRECS', '10' UNION ALL
    SELECT 'IZ_DEFAULTVIEW', 'month' UNION ALL
    SELECT 'IZ_ITEMCOLORS', 'c' UNION ALL
    SELECT 'cust1', 'none' UNION ALL
    SELECT 'cust2', 'none' UNION ALL
    SELECT 'cust3', 'none' UNION ALL
    SELECT 'cust_pagebg', '' UNION ALL
    SELECT 'cust_calbg', '' UNION ALL
    SELECT 'cust_occbg', '' UNION ALL
    SELECT 'cust_occtext', '' UNION ALL
    SELECT 'cust_blankbg', '' UNION ALL
    SELECT 'cust_blanktext', '' UNION ALL
    SELECT 'cust_todaybg', '' UNION ALL
    SELECT 'cust_todaytext', '' UNION ALL
    SELECT 'cust_fillerdays', '' UNION ALL
    SELECT 'cust_calwrappertext', '' UNION ALL
    SELECT 'cust_boxbg', '' UNION ALL
    SELECT 'cust_boxtext', '' UNION ALL
    SELECT 'cust_othertext', ''")) { $error .= mssql_get_last_message().'<br />'."\n"; }
}
} else {
  // Directly accessed
  header('Location: ../');
} ?>